﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.Public;
using YJKOpeningDesignInput.Public;

namespace YJKOpeningDesignInput.EquipCollector
{
	public class EquipMentCollector
	{
		public EquipMentCollector(Document doc)
		{
			this.m_doc = doc;
			this.m_lstRevitLinkIns = CommonInterface.GetLinkRevitIns(doc);
			this.m_lstEquipData = new List<EquipData>();
			this.ReadEquipCollectorXml();
		}

		public void GetEquips(ref MEPData mepData)
		{
			this.GetEquip(ref mepData, this.m_doc, null);
			foreach (RevitLinkInstance revitLinkInstance in this.m_lstRevitLinkIns)
			{
				Document linkDocument = revitLinkInstance.GetLinkDocument();
				if (linkDocument != null)
				{
					this.GetEquip(ref mepData, linkDocument, revitLinkInstance);
				}
			}
		}

		private void GetEquip(ref MEPData mepData, Document doc, RevitLinkInstance revitLinkIns)
		{
			string input = string.Empty;
			foreach (EquipData equipData in this.m_lstEquipData)
			{
				string strPattern = equipData.m_strPattern;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				filteredElementCollector.OfCategory(equipData.m_category).WhereElementIsNotElementType();
				foreach (Element element in filteredElementCollector)
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (familyInstance != null)
					{
						input = this.GetStrFamilyName(familyInstance);
						string strFiName;
						if (Regex.IsMatch(input, strPattern) && (strFiName = equipData.m_strFiName) != null)
						{
							EEquipType equipType;
							if (!(strFiName == "消火栓"))
							{
								if (!(strFiName == "电气设备") && !(strFiName == "通讯设备") && !(strFiName == "火警设备"))
								{
									if (!(strFiName == "分集水器"))
									{
										continue;
									}
									equipType = EEquipType.eWaterCollectorEquipment;
								}
								else
								{
									equipType = EEquipType.eElectricalEquipment;
								}
							}
							else
							{
								equipType = EEquipType.eMechanicalEquipment;
							}
							StructFiBindingData value = new StructFiBindingData(equipType, revitLinkIns);
							mepData.m_dictionaryEquip.Add(familyInstance, value);
						}
					}
				}
			}
		}

		private bool ReadEquipCollectorXml()
		{
			string text = Product.DataLocation + "\\EquipCollector.xml";
			if (!File.Exists(text))
			{
				return false;
			}
			XmlReader xmlReader = null;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlReader = XmlReader.Create(text, new XmlReaderSettings
				{
					IgnoreComments = true
				});
				xmlDocument.Load(xmlReader);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("DataItems");
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					string value = xmlNode2.Attributes["Category"].Value;
                    BuiltInCategory category = (BuiltInCategory)Convert.ToInt32(value);
					string value2 = xmlNode2.Attributes["StrKeyword"].Value;
					string value3 = xmlNode2.Attributes["StrFamilyName"].Value;
					EquipData equipData = new EquipData(category, value2, value3);
					if (equipData == null)
					{
						YJKMessageBox.Show("读取EquipCollectorXml文件异常：Category=" + value);
					}
					else
					{
						this.m_lstEquipData.Add(equipData);
					}
				}
			}
			catch (Exception)
			{
				if (xmlReader != null)
				{
					xmlReader.Close();
					xmlReader = null;
				}
				YJKMessageBox.Show("读取EquipCollectorXml文件异常");
			}
			if (xmlReader != null)
			{
				xmlReader.Close();
			}
			return true;
		}

		private string GetStrFamilyName(FamilyInstance fiIns)
		{
			if (fiIns == null)
			{
				return "";
			}
			FamilySymbol symbol = fiIns.Symbol;
			if (symbol == null)
			{
				return "";
			}
			Family family = symbol.Family;
			if (family == null)
			{
				return "";
			}
			return family.Name;
		}

		private Document m_doc;

		private List<EquipData> m_lstEquipData;

		private List<RevitLinkInstance> m_lstRevitLinkIns;
	}
}
